package top.likeqc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.likeqc.dto.AnswerDTO;
import top.likeqc.dto.PaperAnswerDTO;
import top.likeqc.dto.PaperAnswerDTO0;
import top.likeqc.dto.PaperAnswerDTO2;
import top.likeqc.entity.Answer;
import top.likeqc.entity.Grade;
import top.likeqc.entity.SysUser;
import top.likeqc.mapper.AnswerMapper;
import top.likeqc.mapper.PaperMapper;
import top.likeqc.service.IAnswerService;
import top.likeqc.util.SecurityUtil;
import top.likeqc.vo.AnswerCountVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 答案 服务实现类
 * </p>
 *
 * @author likeqc
 * @since 2022-04-16
 */
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements IAnswerService {

    @Autowired
    SecurityUtil securityUtil;

    @Autowired
    AnswerMapper answerMapper;

    @Autowired
    PaperMapper paperMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(List<Answer> answerList) {
        SysUser sysUser = securityUtil.getSysUser();

        // 不是学生用户，不提交答案
        if (!sysUser.hasRoleStudent()) {
            // return;
        }

        List<Long> questionIds = new ArrayList<>();
        answerList.forEach(answer -> {
            questionIds.add(answer.getQuestionId());
            answer.setUserId(sysUser.getId());
        });

        // 删除旧答案
        remove(new QueryWrapper<Answer>().eq("user_id", sysUser.getId()).in("question_id", questionIds));
        // 保存新的答案
        saveBatch(answerList);

    }

    @Override
    public PaperAnswerDTO getPaperAnswerByPaperId(Long paperId) {
        PaperAnswerDTO data = new PaperAnswerDTO();

        List<PaperAnswerDTO0> paperAnswerDTO0List = answerMapper.getPaperAnserByPaperId(paperId, null);

        if (null != paperAnswerDTO0List && paperAnswerDTO0List.size() > 0) {
            data.setTitle(paperAnswerDTO0List.get(0).getTitle());

            Map<String, AnswerDTO> map = new HashMap<>();   // key：学号


            paperAnswerDTO0List.forEach(row -> {

                if (!map.containsKey(row.getNumber())) {
                    AnswerDTO answerDTO = new AnswerDTO();
                    answerDTO.setStudentNumber(row.getNumber());
                    answerDTO.setStudentName(row.getName());
                    map.put(row.getNumber(), answerDTO);
                }
                AnswerDTO answerDTO = map.get(row.getNumber());

                answerDTO.getAnswerList().add(new AnswerDTO.Answer(row.getContent(), row.getValue()));

            });

            map.values().forEach(answerDTO -> answerDTO.calcAverage());
            data.setAnswerDTOList((map.values().stream().collect(Collectors.toList())));

            // 查询问卷各问题不统计情况
            data.setAnswerCountVOList(answerMapper.getAnswerCountVOList(paperId));
        }

        return data;
    }

    @Override
    public List<PaperAnswerDTO2> getPaperAnswerByPaperId2(Long paperId) {
        List<Grade> gradeList = paperMapper.getGradeListByPaperId(paperId);

        if (gradeList == null || gradeList.isEmpty()) {
            return null;
        }

        List<PaperAnswerDTO2> data = new ArrayList<>();
        gradeList.forEach(grade -> {
            PaperAnswerDTO2 paperAnswerDTO2 = new PaperAnswerDTO2();
            data.add(paperAnswerDTO2);
            paperAnswerDTO2.setGradeName(grade.getName());

            // 班级获取学生答题列表
            List<PaperAnswerDTO0> paperAnswerDTO0List = answerMapper.getPaperAnserByPaperId(paperId, grade.getId());
            Map<String, AnswerCountVO> answerCountVOMap = new HashMap<>();
            paperAnswerDTO0List.forEach(answer -> {
                String content = answer.getContent();
                Integer value = answer.getValue();

                if (!answerCountVOMap.containsKey(content)) {
                    AnswerCountVO answerCountVO = new AnswerCountVO();
                    answerCountVO.setContent(answer.getContent());
                    answerCountVO.setValue1(0);
                    answerCountVO.setValue2(0);
                    answerCountVO.setValue3(0);
                    answerCountVO.setValue4(0);
                    answerCountVO.setValue5(0);
                    answerCountVOMap.put(content, answerCountVO);
                }

                AnswerCountVO answerCountVO = answerCountVOMap.get(content);
                switch (value) {
                    case 1:
                        answerCountVO.setValue1(answerCountVO.getValue1() + 1);
                        break;
                    case 2:
                        answerCountVO.setValue2(answerCountVO.getValue2() + 1);
                        break;
                    case 3:
                        answerCountVO.setValue3(answerCountVO.getValue3() + 1);
                        break;
                    case 4:
                        answerCountVO.setValue4(answerCountVO.getValue4() + 1);
                        break;
                    case 5:
                        answerCountVO.setValue5(answerCountVO.getValue5() + 1);
                        break;
                    default:
                        break;
                }


            });

            List<AnswerCountVO> collect = answerCountVOMap.values().stream().collect(Collectors.toList());
            collect.forEach(answerCountVO -> {
                int a =
                    answerCountVO.getValue1() + 2 * answerCountVO.getValue2() + 3 * answerCountVO.getValue3() + 4 * answerCountVO.getValue4() + 5 * answerCountVO.getValue5();
                int b = 5 * (answerCountVO.getValue1() + answerCountVO.getValue2() + answerCountVO.getValue3() + answerCountVO.getValue4() + answerCountVO.getValue5());
                answerCountVO.setReach(((double)a / (double)b));
            });
            paperAnswerDTO2.setAnswerCountVOList(collect);
        });

        return data;
    }
}
